import 'dart:ui';

import 'package:flame/components.dart';
import 'package:flutter/cupertino.dart';

class TextComponent {
  final Vector2 position;
  String text;
  final Color textColor;
  double textSize;
  final Path path = Path();

  TextComponent(
      {required this.position,
      required this.text,
      required this.textColor,
      required this.textSize});

  void render(Canvas canvas) {
    var textPainter = TextPainter(
        text: TextSpan(
            text: text, style: TextStyle(fontSize: textSize, color: textColor)),
        textAlign: TextAlign.center,
        textDirection: TextDirection.ltr);
    textPainter.layout();
    textPainter.paint(
        canvas,
        Offset(position.x - textPainter.width / 2,
            position.y - textPainter.height / 2));
    path.reset();
    path.addRect(Rect.fromLTWH(
        position.x - textPainter.width / 2,
        position.y - textPainter.height / 2,
        textPainter.width,
        textPainter.height));
  }
}
